If the
SpringApplicationdefaults are not to your taste, you can instead create a local instance and customize it. For example, to turn off the banner, you could write:
如果使用者對SpringApplication 的預設不滿意的話,可以自己創建一個實體並為其客製屬性。
譬如說下方的程式碼就把Banner 關掉。
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
}
}
A
SpringApplicationhas bean property setters, so you can use its Java API as you create the application to modify its behavior. Alternatively, you can externalize the configuration by setting properties inspring.main.*. For example, inapplication.properties, you might have the following settings:
SpringApplication 有 bean 屬性設置方法,可以透過 Java API 在創建 application 的時候去修改他的行爲。除了透過 SpringApplication setter 方法之外,你可以透過外掛註冊(externalize the configuration) 以 spring.main.* 開頭的屬性來取代一系列的 setter 方法。
下方為一個透過客製化外部註冊的application.yml
spring:
main:
web-application-type: "none"
banner-mode: "off"
上述分別對應 SpringApplication 的 setWebApplicationType 、setBannerMode
這裡特別注意到 web-application-type (歸類在spring.main.*),這個屬性有對應的enum 類別,
有三個列舉常數(enum constant)
| enum constant | description |
|---|---|
| NONE | The application should not run as a web application and should not start an embedded web server. |
| REACTIVE | The application should run as a reactive web application and should start an embedded reactive web server. |
| SERVLET | The application should run as a servlet-based web application and should start an embedded servlet web server. |
上表對應的三種,NONE 代表不作為 web 應用,自然也不會啟動內崁的web server ;
REACTIVE 就是啟動的是響應式應用,以及對應的內崁的響應式web server,最後一種
SERVLET 就是一般的 web 應用 以及對應的內崁 web server( 譬如說 Tomcat )。
那在一般我們沒有客製化 SpringApplication 的情況下,預設是採用哪一種 web-application-type ?
這得參照官方文檔的 .A.1. Core Properties (參照下方參考) 中 關於 spring.main.web-application-type 的描述,
Flag to explicitly request a specific type of web application. If not set, auto-detected based on the classpath.
意思是這個註冊屬性值,指定web 應用的類型,若沒有特別設定的話,會自動根據 類別路徑(classpath)去偵測,這裡的意思是…. 如果去使用者有特別注入特定的web server 的話,就會將應用識別成對應的類型,譬如說有內崁入的tomcat server 就會識別為 web-application-type 就會識別為 SERVLET。
參考資料
{官方} Externalize the Configuration of SpringApplication
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto
{官方} SpringApplication JavaDoc
https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringApplication.html
{官方} Enum Class WebApplicationType
https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/WebApplicationType.html
{官方} .A.1. Core Properties
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix.application-properties.core